package com.linewow.xhyy.nihaonew.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.linewow.xhyy.nihaonew.R;
import com.linewow.xhyy.nihaonew.entity.country.CountryInfo;
import com.linewow.xhyy.nihaonew.util.CommonUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by LXR on 2016/10/20.
 */
public class CountryAdapter extends BaseAdapter implements SectionIndexer {
    private Context context;
    private List<CountryInfo> countryInfos;
    private boolean chineseFlag;

    public CountryAdapter(Context context, List<CountryInfo> countryInfos) {
        this.context = context;
        this.countryInfos = countryInfos;
        this.chineseFlag= CommonUtil.ChineseLocale(context);
    }

    @Override
    public int getCount() {
        return countryInfos.size();
    }

    @Override
    public Object getItem(int i) {
        return countryInfos.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if(view==null){
            view= View.inflate(context, R.layout.coogame_country_item, null);
            viewHolder=new ViewHolder(view);
            view.setTag(viewHolder);
        }else{
            viewHolder= (ViewHolder) view.getTag();
        }

        CountryInfo info=countryInfos.get(i);
        // 根据position获取分类的首字母的Char ascii值
        int section = getSectionForPosition(i);
        if(i==getPositionForSection(section)){
            viewHolder.countryCatalog.setVisibility(View.VISIBLE);
            viewHolder.countryCatalog.setText(info.index);
        }else{
            viewHolder.countryCatalog.setVisibility(View.GONE);
        }
        viewHolder.countryName.setText(chineseFlag?info.countryName:info.abbreviation);
        return view;
    }

    @Override
    public Object[] getSections() {
        return null;
    }

    @Override
    public int getPositionForSection(int section) {
        if (section != 42)
        {
            for (int i = 0; i < getCount(); i++)
            {
                String sortStr = countryInfos.get(i).index;
                char firstChar = sortStr.toUpperCase(Locale.CHINESE).charAt(0);
                if (firstChar == section)
                {
                    return i;
                }
            }
        }
        else
        {
            return 0;
        }

        return -1;
    }

    @Override
    public int getSectionForPosition(int i) {
        return countryInfos.get(i).index.charAt(0);
    }

    static class ViewHolder {
        @Bind(R.id.country_catalog)
        TextView countryCatalog;
        @Bind(R.id.country_name)
        TextView countryName;
        @Bind(R.id.country_number)
        TextView countryNumber;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    public void updateListView(List<CountryInfo> list)
    {
        if (list == null)
        {
            this.countryInfos = new ArrayList<CountryInfo>();
        }
        else
        {
            this.countryInfos = list;
        }
        notifyDataSetChanged();
    }
}
